<template>
	<div style="height: 100%">
		<div>
			<div>
				<zh-tree
					ref="treeRef"
					show-checkbox
					:height="`calc(100vh - 50px)`"
					:checkStrictly="false"
					:autoExpandParent="state.autoExpandParent"
					:default-expanded-keys="state.expandedKeys"
					:default-checked-keys="state.checkedKeys"
					:data="state.routerFuncList"
					:props="{ children: 'children', label: 'title' }"
					node-key="key"
				>
				</zh-tree>
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup name="RouterFuncTree">
import { ElTree,ElMessage } from 'element-plus';
const VITE_APP_ID = import.meta.env.VITE_APP_ID as string;
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	userId: {
		type: String,
		default: null,
		required: false,
	},
	roleId: {
		type: String,
		default: null,
		required: false,
	},
	type: {
		type: String,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue','refresh']);

//------------------定义子组件---------------------------
const treeRef = ref<InstanceType<typeof ElTree>>();

//------------------内部变量定义-------------------------
const state = reactive({
	description: '路由功能管理',
	// 功能树
	expandedKeys: [],
	selectedKeys: [],
	searchValue: '',
	autoExpandParent: true,
	routerFuncList: [] as any,
	checkedKeys: [],
});

//------------------定义调用方法-------------------------
function loopExpandKey(data, keys, expandKeys) {
	let flag = false;
	data.forEach((item) => {
		let itemFlag = false;
		const myKey = item.key;
		keys.some((item) => {
			if (myKey === item) {
				itemFlag = true;
				return true;
			}
		});
		if (item.children) {
			const childFlag = loopExpandKey(item.children, keys, expandKeys);
			if (childFlag) {
				itemFlag = true;
			}
		}
		if (itemFlag) {
			flag = true;
			expandKeys.push(item.key);
		}
	});
	return flag;
}
function findRouterFuncTreeList() {
	let funCode = 'router0009';
    if (props.type === 'myMenu') {
        funCode = 'router0031';
    }
	const params = {
		searchText: state.searchValue,
		roleId: props.roleId,
		userId: props.userId,
	};
	setTimeout(() => {
		$api.common.send
			.post(funCode, params, 30000)
			.then((res) => {
				const data = [...res.result.treeList];
				if (data) {
					state.routerFuncList = data;
					state.checkedKeys = [];
					const expandedKeys = res.result.expandedKeys;
					const checkedKeys = res.result.checkedKeys;
					if (checkedKeys && checkedKeys.length > 0) {
						// 设置展开节点
						const expandedRouterFuncKeys = [];
						loopExpandKey(data, checkedKeys, expandedRouterFuncKeys);
						state.checkedKeys = checkedKeys;
						state.expandedKeys = expandedRouterFuncKeys;
					} else {
						state.expandedKeys = expandedKeys;
					}
					state.autoExpandParent = true;
				}
			})
			.catch(() => {});
	}, 50);
}

function getCheckedKeys() {
	return treeRef.value!.getCheckedKeys(false);
}

function saveRouterFunc() {
	let funcCode = '';
	let checkedkeys: any = getCheckedKeys();
	const params: any = {};
	if (props.type === 'user') {
		funcCode = 'user0021';
		params.userId = props.userId;
	} else if (props.type === 'role') {
		funcCode = 'role0012';
		params.roleId = props.roleId;
	}else if (props.type === 'myMenu') {
		funcCode = 'userMenu0010';
		checkedkeys = getCheckedChildKeys();
		params.appId=VITE_APP_ID;
		if(checkedkeys.length>5){
			ElMessage.warning('选择的功能不能超过4个');
			return ;
		}
	}

	

	if (Array.isArray(checkedkeys)) {
		params.objectIds = checkedkeys;
	} else {
		params.objectIds = checkedkeys.checked;
	}
	setTimeout(() => {
		$api.common.send
			.post(funcCode, params, 20000)
			.then(() => {
				if (props.type === 'myMenu') {
					ElMessage.success('保存成功');
					emit('refresh');
				}else{
					ElMessage.success('授权成功');
				}
				
				emit('update:modelValue', false);
			})
			.catch(() => {});
	}, 10);
}
function getCheckedChildKeys() {
	return treeRef.value!.getCheckedKeys(true);
}
//------------------初始化------------------------------
onMounted(() => {
	nextTick(() => {
		findRouterFuncTreeList();
	});
});

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({
	getCheckedKeys,
	saveRouterFunc,
});
</script>
<style></style>
